package com.weimob.mcloud.wechatwork.customer.convertor.sidebar;

import com.weimob.mcloud.wechatwork.common.pojo.vo.BasicInfoVO;
import com.weimob.mcloud.wechatwork.common.utils.CollUtils;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.WxChannelInfoBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.distribution.response.QueryRewardGoodsListResponse;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.distribution.response.bo.CommissionBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.bo.GoodsUrlBO;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BaseRequest;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.sidebar.SidebarDistributionGoodsBO;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.function.Function;

/**
 * @author jun.liu05
 * @since 1.0
 */
public class SidebarConvertor {

    public static <T extends BaseRequest> BasicInfoVO convertBasicInfo(T baseRequest) {
        if (Objects.isNull(baseRequest) || Objects.isNull(baseRequest.getBasicInfo())) {
            return new BasicInfoVO();
        }

        BasicInfo basicInfo = baseRequest.getBasicInfo();
        BasicInfoVO basicInfoVO = new BasicInfoVO();
        basicInfoVO.setBosId(basicInfo.getBosId());
        basicInfoVO.setCorpId(basicInfo.getCorpId());
        basicInfoVO.setProductId(basicInfo.getProductInstanceId());
        basicInfoVO.setProductInstanceId(basicInfo.getProductInstanceId());
        basicInfoVO.setVid(basicInfo.getVid());
        basicInfoVO.setVidType(basicInfo.getVidType());

        return basicInfoVO;
    }

    public static List<SidebarDistributionGoodsBO> convertSidebarDistributionGoodsBO(List<QueryRewardGoodsListResponse> queryRewardGoodsListResponseList, List<GoodsUrlBO> goodsUrlBOList, WxChannelInfoBO wxChannelInfoBO) {
        if (CollectionUtils.isEmpty(queryRewardGoodsListResponseList)) {
            return Collections.emptyList();
        }

        Map<Long, GoodsUrlBO> map = CollUtils.toMap(goodsUrlBOList, GoodsUrlBO::getGoodsId, Function.identity());
        List<SidebarDistributionGoodsBO> list = new ArrayList<>();
        for (QueryRewardGoodsListResponse queryRewardGoodsListResponse : queryRewardGoodsListResponseList) {
            SidebarDistributionGoodsBO sidebarDistributionGoodsBO = new SidebarDistributionGoodsBO();
            sidebarDistributionGoodsBO.setGoodsId(queryRewardGoodsListResponse.getGoodsId());
            sidebarDistributionGoodsBO.setGoodsName(queryRewardGoodsListResponse.getGoodsName());
            sidebarDistributionGoodsBO.setGoodsImgUrl(queryRewardGoodsListResponse.getDefaultImageUrl());
            sidebarDistributionGoodsBO.setAvailableStockNum(queryRewardGoodsListResponse.getAvailableStock());
            sidebarDistributionGoodsBO.setMinSalePrice(queryRewardGoodsListResponse.getMinSalePrice());
            sidebarDistributionGoodsBO.setCommissionType(queryRewardGoodsListResponse.getCommissionType());

            if (CollectionUtils.isNotEmpty(queryRewardGoodsListResponse.getCommissionList())) {
                CommissionBO commissionBO = queryRewardGoodsListResponse.getCommissionList().get(0);
                // 设置微客预计最小奖励
                sidebarDistributionGoodsBO.setMinCommission(commissionBO.getPreWeikeMinReward());
            }

            if (map.containsKey(queryRewardGoodsListResponse.getGoodsId()) && Objects.nonNull(wxChannelInfoBO)){
                GoodsUrlBO goodsUrlBO = map.get(queryRewardGoodsListResponse.getGoodsId());
                sidebarDistributionGoodsBO.setH5Url(goodsUrlBO.getH5Url());
                sidebarDistributionGoodsBO.setMiniUrl(goodsUrlBO.getMiniUrl());
                sidebarDistributionGoodsBO.setAppId(wxChannelInfoBO.getOriginId());
            }
            list.add(sidebarDistributionGoodsBO);
        }

        return list;
    }
}
